<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>重置密码</title>
    <!-- 引入Bootstrap CSS -->
    <link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
    <link rel="icon" href="{{ url_for('static', filename='ico.svg') }}" type="image/x-icon">
    <style>
        body {
            background-image: url('/random_image');
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            background-attachment: fixed;
        }
        .card {
            background-color: rgba(255, 255, 255, 0.8);
        }
        .toggle-password {
            cursor: pointer;
            position: absolute;
            right: 15px;
            top: 38px;
            color: #999;
        }
        .version {
            color: #6c757d;
            padding: 10px;
            font-size: 14px;
            text-align: center;
            background-color: rgba(255, 255, 255, 0.8);
            position: fixed;
            bottom: 0;
            width: 100%;
        }
        .version p {
            margin: 0;
        }
    </style>
</head>
<body>
    <!-- 显示闪现消息 -->
    {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
            <div class="container mt-3">
                {% for category, message in messages %}
                    <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                        {{ message }}
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="关闭"></button>
                    </div>
                {% endfor %}
            </div>
        {% endif %}
    {% endwith %}

    <div class="container d-flex justify-content-center align-items-center" style="min-height: 80vh;">
        <div class="card p-4" style="width: 400px; position: relative;">
            <div class="text-center">
                <!-- Logo -->
                <img src="{{ url_for('static', filename='logo.png') }}" alt="Logo" style="max-width: 100px;">
                <h3>重置密码</h3>
            </div>

            {% if reset_username %}
                <!-- 模态框触发按钮（隐藏） -->
                <button type="button" id="modalTrigger" class="btn btn-primary d-none" data-bs-toggle="modal" data-bs-target="#usernameModal">
                    显示用户名
                </button>

                <!-- 模态框 -->
                <div class="modal fade" id="usernameModal" tabindex="-1" aria-labelledby="usernameModalLabel" aria-hidden="true">
                  <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                      <div class="modal-header">
                        <h5 class="modal-title" id="usernameModalLabel">密码重置成功</h5>
                      </div>
                      <div class="modal-body">
                        <p>您的用户名是：<strong>{{ reset_username }}</strong></p>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-primary w-100" onclick="redirectToLogin()">我知道了</button>
                      </div>
                    </div>
                  </div>
                </div>
            {% else %}
                <form method="post" action="{{ url_for('forgot_password') }}">
                    <div class="mb-3 position-relative">
                        <label for="security_code" class="form-label">安全码</label>
                        <input type="text" class="form-control" id="security_code" name="security_code" placeholder="输入安全码" required>
                    </div>
                    <div class="mb-3 position-relative">
                        <label for="new_password" class="form-label">新密码</label>
                        <input type="password" class="form-control" id="new_password" name="new_password" placeholder="输入新密码" required>
                        <span toggle="#new_password" class="toggle-password" onclick="togglePassword('new_password')">显示</span>
                    </div>
                    <div class="mb-3 position-relative">
                        <label for="confirm_password" class="form-label">确认新密码</label>
                        <input type="password" class="form-control" id="confirm_password" name="confirm_password" placeholder="再次输入新密码" required>
                        <span toggle="#confirm_password" class="toggle-password" onclick="togglePassword('confirm_password')">显示</span>
                    </div>
                    <button type="submit" class="btn btn-primary w-100">重置密码</button>
                    <div class="text-center mt-3">
                        <a href="{{ url_for('login') }}">返回登录</a>
                    </div>
                </form>
            {% endif %}
        </div>
    </div>

    <div class="version">
        <p>项目名称：<strong>alist-strm</strong></p>
        <p>作者：<strong><a href="https://www.tefuir0829.cn" target="_blank">tefuir</a></strong></p>
        <p>版本：{{ g.local_version }}</p>
    </div>

    <!-- 引入Bootstrap JS -->
    <script src="{{ url_for('static', filename='js/bootstrap.bundle.min.js') }}"></script>
    <script>
        function togglePassword(id) {
            var input = document.getElementById(id);
            var toggle = input.nextElementSibling;
            if (input.type === "password") {
                input.type = "text";
                toggle.textContent = "隐藏";
            } else {
                input.type = "password";
                toggle.textContent = "显示";
            }
        }

        function redirectToLogin() {
            window.location.href = "{{ url_for('login') }}";
        }

        // 如果存在重置的用户名，自动触发模态框
        {% if reset_username %}
            var modalTrigger = document.getElementById('modalTrigger');
            modalTrigger.click();
        {% endif %}
    </script>
</body>
</html>
